<?php
/**
 * @filesource:					NodeAction.class.php
 * @desc:						操作节点模块管理
 * 
 * @author:						houyining
 * @copyright:					hou.yining@gmail.com	
 * @date:						2011-04-12
 */

class NodeAction extends CommonAction {
	public function index(){
		$Node = new NodeModel();
		
		if(isset($_REQUEST['pid']) && !empty($_REQUEST['pid'])){
			$this->assign('tag', L('list_sub_node'));
			$map = array('pid' => $_REQUEST['pid']);
			$crumb_info = array(
				array('uri' => '__URL__', 'title' => L('node_manage'), 'sort' => C('CRUMB_DEFAULT')),
				array('uri' => '__URL__/index/pid/' . $_REQUEST['pid'], 'title' => L('list_sub_node'), 'sort' => C('CRUMB_LAST') ),
			);	
			$this->_action_link = array(
				array('uri' => '__URL__/index', 'title' => L('list_node')),
				array('uri' => '__URL__/add/pid/' . $_REQUEST['pid'], 'title' => L('add_node_level')),
			);
			$this->assign('add_node', L('add_node_level'));
		}else{
			$this->assign('tag', L('list_node'));
			$map = array('level' => 2);
			$crumb_info = array(
				array('uri' => '__URL__', 'title' => L('node_manage'), 'sort' => C('CRUMB_LAST')),
			);	
			$this->_action_link = array(
				array('uri' => '__URL__/add', 'title' => L('add_node')),
			);
			$this->assign('add_node', L('add_node'));
		}

		if (! empty ( $Node )) {
			$this->_list ( $Node, $map );
		}
		
		$this->_mergeCrumbInfo($crumb_info);
		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		$this->display();
		return;
	}
	
	public function add(){	
		//获取父节点id
		if(isset($_REQUEST['pid']) && !empty($_REQUEST['pid'])){
			$parent_id = $_REQUEST['pid'];	
			$crumb_info = array(
				array('uri' => '__URL__', 'title' => L('node_manage'), 'sort' => C('CRUMB_DEFAULT')),
				array('uri' => '__URL__/add/pid/' . $parent_id, 'title' => L('addsub'), 'sort' => C('CRUMB_LAST')),
			);
			$this->_mergeCrumbInfo($crumb_info);
			
			$this->_action_link = array(
			array('uri' => '__URL__/index/pid/' . $parent_id, 'title' => L('list_sub_node')),
			);
		}else{
			$crumb_info = array(
				array('uri' => '__URL__', 'title' => L('node_manage'), 'sort' => C('CRUMB_DEFAULT')),
				array('uri' => '__URL__/add', 'title' => L('add_node'), 'sort' => C('CRUMB_LAST')),
			);
			$this->_mergeCrumbInfo($crumb_info);
			
			$this->_action_link = array(
			array('uri' => '__URL__/index/', 'title' => L('list_node')),
			);
			$Node = new NodeModel();
			$rootNodeId = $Node->getRootNodeId();
			$parent_id = $rootNodeId;
		}
		
		$this->assign('add_node', L('add_node'));
		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		$this->assign('group_list', $this->_getGroupList());
		$this->assign('parent_id', $parent_id);
		parent::add();
	}
	
	public function edit(){
		$id = $_REQUEST['id'];
		$crumb_info = array(
			array('uri' => '__URL__', 'title' => L('node_manage'), 'sort' => C('CRUMB_DEFAULT')),
			array('uri' => '__URL__/edit/id/' . $id, 'title' => L('edit_node'), 'sort' => C('CRUMB_LAST')),
		);

		$this->_mergeCrumbInfo($crumb_info);
		$this->_action_link = array(array('uri' => '__URL__/index', 'title' => L('list_node')),
		);
		
		$this->assign('crumb_info', $this->_crumb_info);
		$this->assign('action_link', $this->_action_link);
		$this->assign('group_list', $this->_getGroupList());
		
		$Node = new NodeModel();
		$vo = $Node->getById ( $id );
		$this->assign('parent_id', $vo['pid']);
		$this->assign ( 'vo', $vo );
		
		$this->display ();
	}
	
	public function insert(){
		$Node = new NodeModel();		
		$level = $Node->getLevel($_POST['pid']);
		$_POST['level'] = $level;
		
		parent::insert();
	}
	
	private function _getGroupList()
	{
		$Group = M('Group');
		$list  = $Group->where('status=1')->field('id, title')->select();
		return $list;
	}
}
?>